#!/usr/bin/python
# -*- coding: UTF-8 -*-
class friend:
    def __init__(self):
        self.__list=[]
        self.a=0
    def __iter__(self):
        '''
        外部用for循环迭代或列表生成器的时候调用本方法
        :return:
        '''
        return self
    def next(self):
        self.a=self.a+1
        if self.a>100:
            raise StopIteration()
        return self.a

    def __getitem__(self, item):
        '''
        外部通过friend()[2]访问时调用
        :param item:
        :return:
        '''
        return item

    def __setitem__(self, key, value):
        '''
        外部用friend()[key]=value时调用
        :param key:
        :param value:
        :return:
        '''
        self.__list[key]=value

    def __getattr__(self, item):
        '''
        当调用到没有到属性时才走这个方法
        :param item:
        :return:
        '''
        if item=='haha':
            return 77

    def __call__(self, *args, **kwargs):
        '''
        f=friend()
        f()的时候调用本方法
        :param args:
        :param kwargs:
        :return:
        '''
        print "直接调用实例"
if __name__ == '__main__':
    for x in friend():
        print x
    print [x **2 for x in friend() if x%5==0]

    print friend()[2]
    # print friend()[2:5]
    print friend().haha
    print callable(friend())